package cn.hznc.controller.service.toolAnalyze;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.toolAnalyze.PersonnelUsageAnalysisReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.mapper.*;
import cn.hznc.repository.AssemblyToolInventoryRepository;
import cn.hznc.repository.EntryExitRecordRepository;
import cn.hznc.repository.IcabinetRecordRepository;
import cn.hznc.repository.ToolForkInfoRespository;
import cn.hznc.utils.CommonUtil;
import cn.hznc.vo.*;
import cn.hznc.vo.personalrecord.PersonalRecordDateVo;
import cn.hznc.vo.personalrecord.PersonalRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PersonnelUsageAnalysisService {
    @Resource
    private EntryExitRecordMapper entryExitRecordMapper;
    @Resource
    private ToolPartMapper toolPartMapper;
    @Resource
    private MinioProp minioProp;
    @Resource
    private TypeItemMapper typeItemMapper;
    @Resource
    private ToolForkInfoMapper toolForkInfoMapper;
    @Resource
    private ICabinetRecordMapper iCabinetRecordMapper;
    @Resource
    private AssemblyToolInventoryMapper assemblyToolInventoryMapper;

    @Resource
    private IcabinetRecordRepository icabinetRecordRepository;

    @Resource
    private EntryExitRecordRepository entryExitRecordRepository;

    @Resource
    private ToolForkInfoRespository toolForkInfoRespository;

    @Resource
    private AssemblyToolInventoryRepository assemblyToolInventoryRepository;

    public PageResult<PersonnelUsageAnalysisEntity> queryPersonnelUsageConsume(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {
        List<Integer> integers = new ArrayList<>();
        int flag = 0;
        int startPage = (personnelUsageAnalysisReq.getPageNo() - 1) * personnelUsageAnalysisReq.getPageSize();
        int endPage = personnelUsageAnalysisReq.getPageNo() * personnelUsageAnalysisReq.getPageSize();
        List<PersonnelUsageAnalysisEntity> personnelUsageAnalysisEntityList = new ArrayList<>();
        if (StringUtils.isNotEmpty(personnelUsageAnalysisReq.getType()) && personnelUsageAnalysisReq.getType().equals("1")) {
            LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (personnelUsageAnalysisReq.getStartTime() != null && personnelUsageAnalysisReq.getEndTime() != null) {
                if (personnelUsageAnalysisReq.getStartTime().equals(personnelUsageAnalysisReq.getEndTime())) {
                    Date date = personnelUsageAnalysisReq.getEndTime();
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(date);
                    calendar.add(Calendar.DATE, 1);
                    date = calendar.getTime();
                    personnelUsageAnalysisReq.setEndTime(date);
                }
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, personnelUsageAnalysisReq.getStartTime(), personnelUsageAnalysisReq.getEndTime());
            } else {
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, LocalDate.now().minusMonths(1), LocalDate.now());
            }
            lambdaQueryWrapper.ne(EntryExitRecordEntity::getPartno, "");
            if (CollectionUtil.isNotEmpty(personnelUsageAnalysisReq.getPeopleList()))
                lambdaQueryWrapper.in(EntryExitRecordEntity::getOperator, personnelUsageAnalysisReq.getPeopleList());
            lambdaQueryWrapper.eq(EntryExitRecordEntity::getType, "outbound");
            List<EntryExitRecordEntity> entryExitRecordEntityList1 = entryExitRecordMapper.selectList(lambdaQueryWrapper);
            Map<String, List<EntryExitRecordEntity>> map = entryExitRecordEntityList1.stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getPartno));
            Iterator<Map.Entry<String, List<EntryExitRecordEntity>>> entries = map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, List<EntryExitRecordEntity>> entry = entries.next();
                Map<Object, List<EntryExitRecordEntity>> map0 = entry.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getWarehouseName())));
                Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries2 = map0.entrySet().iterator();
                while (entries2.hasNext()) {
                    Map.Entry<Object, List<EntryExitRecordEntity>> entry2 = entries2.next();
                    Map<Object, List<EntryExitRecordEntity>> map5 = entry2.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getOperator())));
                    Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries3 = map5.entrySet().iterator();
                    integers.add(map5.size());
                    if (personnelUsageAnalysisEntityList.size() < personnelUsageAnalysisReq.getPageSize()) {
                        while (entries3.hasNext()) {
                            Map.Entry<Object, List<EntryExitRecordEntity>> entry3 = entries3.next();

                            if (flag < endPage) {
                                flag++;
                            } else {
                                break;
                            }
                            if (flag > startPage) {
                                PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity = new PersonnelUsageAnalysisEntity();
                                personnelUsageAnalysisEntity.setLocation(entry2.getKey());
                                personnelUsageAnalysisEntity.setOperator(entry3.getKey());
                                personnelUsageAnalysisEntity.setType("刀具");
                                personnelUsageAnalysisEntity.setPartno(entry.getKey());
                                personnelUsageAnalysisEntity.setDate(entry3.getValue().get(0).getCreateTime());
                                LambdaQueryWrapper<ToolForkInfoEntity> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper3.eq(ToolForkInfoEntity::getPartno, entry.getKey());
                                lambdaQueryWrapper3.ne(ToolForkInfoEntity::getIdnr, "");
                                lambdaQueryWrapper3.ne(ToolForkInfoEntity::getType, "");
                                ToolForkInfoEntity toolPartEntities1 = toolForkInfoMapper.selectOne(lambdaQueryWrapper3);
//                        LambdaQueryWrapper<ToolPartEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
//                        lambdaQueryWrapper1.eq(ToolPartEntity::getPartno, entry.getKey());
//                        List<ToolPartEntity> toolPartEntityList = toolPartMapper.selectList(lambdaQueryWrapper1);
                                if (ObjectUtil.isNotEmpty(toolPartEntities1)) {
                                    personnelUsageAnalysisEntity.setIndr(toolPartEntities1.getIdnr());
                                    personnelUsageAnalysisEntity.setTypeName(typeItemMapper.getTypeNameByType(toolPartEntities1.getType()));

                                }

//                                Map<String, List<EntryExitRecordEntity>> map1 = entry3.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));

                                try {
                                    personnelUsageAnalysisEntity.setConsumeNum(entry3.getValue().stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));
                                } catch (Exception e) {
                                }
                                personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity);
                            }

                        }

                    }

                }
            }
        }
        if (StringUtils.isNotEmpty(personnelUsageAnalysisReq.getType()) && personnelUsageAnalysisReq.getType().equals("0")) {
            LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (personnelUsageAnalysisReq.getStartTime() != null && personnelUsageAnalysisReq.getEndTime() != null) {
                if (personnelUsageAnalysisReq.getStartTime().equals(personnelUsageAnalysisReq.getEndTime())) {
                    Date date = personnelUsageAnalysisReq.getEndTime();
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(date);
                    calendar.add(Calendar.DATE, 1);
                    date = calendar.getTime();
                    personnelUsageAnalysisReq.setEndTime(date);
                }
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, personnelUsageAnalysisReq.getStartTime(), personnelUsageAnalysisReq.getEndTime());
            } else {
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, LocalDate.now().minusMonths(1), LocalDate.now());
            }
            lambdaQueryWrapper.ne(EntryExitRecordEntity::getToolNo, "");
            lambdaQueryWrapper.eq(EntryExitRecordEntity::getType, "outbound");
            if (CollectionUtil.isNotEmpty(personnelUsageAnalysisReq.getPeopleList()))
                lambdaQueryWrapper.in(EntryExitRecordEntity::getOperator, personnelUsageAnalysisReq.getPeopleList());
            List<EntryExitRecordEntity> entryExitRecordEntityList1 = entryExitRecordMapper.selectList(lambdaQueryWrapper);
            Map<String, List<EntryExitRecordEntity>> map = entryExitRecordEntityList1.stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getToolNo));
            Iterator<Map.Entry<String, List<EntryExitRecordEntity>>> entries = map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, List<EntryExitRecordEntity>> entry = entries.next();
                Map<Object, List<EntryExitRecordEntity>> map0 = entry.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getWarehouseName())));
                Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries2 = map0.entrySet().iterator();
                while (entries2.hasNext()) {
                    Map.Entry<Object, List<EntryExitRecordEntity>> entry2 = entries2.next();
                    Map<Object, List<EntryExitRecordEntity>> map5 = entry2.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getOperator())));
                    Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries3 = map5.entrySet().iterator();
                    integers.add(map5.size());
                    if (personnelUsageAnalysisEntityList.size() < personnelUsageAnalysisReq.getPageSize()) {
                        while (entries3.hasNext()) {
                            Map.Entry<Object, List<EntryExitRecordEntity>> entry3 = entries3.next();

                            if (flag < endPage) {
                                flag++;
                            } else {
                                break;
                            }
                            if (flag > startPage) {
                                PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity1 = new PersonnelUsageAnalysisEntity();
                                personnelUsageAnalysisEntity1.setDate(entry3.getValue().get(0).getCreateTime());
                                personnelUsageAnalysisEntity1.setLocation(entry2.getKey());
                                personnelUsageAnalysisEntity1.setOperator(entry3.getKey());
                                personnelUsageAnalysisEntity1.setType("总成");
                                personnelUsageAnalysisEntity1.setPartno(entry.getKey());

                                LambdaQueryWrapper<AssemblyToolInventoryEntity> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper3.eq(AssemblyToolInventoryEntity::getToolNo, entry.getKey());
                                AssemblyToolInventoryEntity toolPartEntityList = assemblyToolInventoryMapper.selectOne(lambdaQueryWrapper3);
                                if (ObjectUtil.isNotEmpty(toolPartEntityList)) {
                                    personnelUsageAnalysisEntity1.setTypeName(toolPartEntityList.getToolType());
                                }

//                                Map<String, List<EntryExitRecordEntity>> map1 = entry3.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));

                                try {
                                    personnelUsageAnalysisEntity1.setConsumeNum(entry3.getValue().stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));
                                } catch (Exception e) {
                                }
                                personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity1);
                            }

                        }

                    }

                }
            }
        }
        PageInfo<PersonnelUsageAnalysisEntity> result = new PageInfo<>(personnelUsageAnalysisEntityList);
        return new PageResult<>(result.getList(), (long) integers.stream().reduce(Integer::sum).orElse(0));
    }

    public Object queryPersonnelUsageConsumeReload(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {
        List<RecordGroupOperatorIndexVo> groupIndexList = new ArrayList<>();
        List<RecordGroupOperatorIndexVo> pageList = new ArrayList<>();
        Map<String, Object> retMap = new HashMap<>();
        if (StringUtils.isNotBlank(personnelUsageAnalysisReq.getType()) && Objects.equals(personnelUsageAnalysisReq.getType(), "1")) {
            groupIndexList = entryExitRecordRepository.selectGroupIndex(personnelUsageAnalysisReq);
            if (CollectionUtil.isNotEmpty(groupIndexList)) {
                pageList = CommonUtil.pageList(groupIndexList, personnelUsageAnalysisReq.getPageNo(), personnelUsageAnalysisReq.getPageSize());
                //设置多余信息
                pageList.forEach(recordGroupOperatorIndexVo -> {
                    recordGroupOperatorIndexVo.setTypeName(toolForkInfoRespository.selectTypeNameByPartno(recordGroupOperatorIndexVo.getPartno()));
                    recordGroupOperatorIndexVo.setIdnr(toolForkInfoRespository.selectPartTypeByPartno(recordGroupOperatorIndexVo.getPartno()));
                });
            }
        } else if (StringUtils.isNotBlank(personnelUsageAnalysisReq.getType()) && Objects.equals(personnelUsageAnalysisReq.getType(), "0")) {
            groupIndexList = entryExitRecordRepository.selectGroupIndexReload(personnelUsageAnalysisReq);
            if (CollectionUtil.isNotEmpty(groupIndexList)) {
                pageList = CommonUtil.pageList(groupIndexList, personnelUsageAnalysisReq.getPageNo(), personnelUsageAnalysisReq.getPageSize());
                //设置多余信息
                pageList.forEach(recordGroupOperatorIndexVo -> {
                    recordGroupOperatorIndexVo.setTypeName(assemblyToolInventoryRepository.selectTypeByToolNo(recordGroupOperatorIndexVo.getToolNo()));
                });
            }
        }
        retMap.put("list", pageList);
        retMap.put("total", groupIndexList.size());
        if (CollectionUtil.isNotEmpty(groupIndexList)) {
            retMap.put("peopleList", groupIndexList.stream().map(RecordGroupOperatorIndexVo::getOperator).distinct().collect(Collectors.toList()));
        } else {
            retMap.put("peopleList", new ArrayList<>());
        }
        return retMap;
        //return new PageResult<>(pageList ,(long)groupIndexList.size());
    }

    @Transactional
    public PageResult<PersonnelUsageAnalysisEntity> queryKnifeInstallation(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {
        List<Integer> integers = new ArrayList<>();
        int flag = 0;
        int startPage = (personnelUsageAnalysisReq.getPageNo() - 1) * personnelUsageAnalysisReq.getPageSize();
        int endPage = personnelUsageAnalysisReq.getPageNo() * personnelUsageAnalysisReq.getPageSize();
        List<PersonnelUsageAnalysisEntity> personnelUsageAnalysisEntityList = new ArrayList<>();
        if (StringUtils.isNotEmpty(personnelUsageAnalysisReq.getType()) && personnelUsageAnalysisReq.getType().equals("1")) {
            LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (personnelUsageAnalysisReq.getStartTime() != null && personnelUsageAnalysisReq.getEndTime() != null) {
                if (personnelUsageAnalysisReq.getStartTime().equals(personnelUsageAnalysisReq.getEndTime())) {
                    Date date = personnelUsageAnalysisReq.getEndTime();
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(date);
                    calendar.add(Calendar.DATE, 1);
                    date = calendar.getTime();
                    personnelUsageAnalysisReq.setEndTime(date);
                }
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, personnelUsageAnalysisReq.getStartTime(), personnelUsageAnalysisReq.getEndTime());
            } else {
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, LocalDate.now().minusMonths(1), LocalDate.now());
            }
            lambdaQueryWrapper.ne(EntryExitRecordEntity::getPartno, "");
            if (CollectionUtil.isNotEmpty(personnelUsageAnalysisReq.getPeopleList()))
                lambdaQueryWrapper.in(EntryExitRecordEntity::getOperator, personnelUsageAnalysisReq.getPeopleList());
            lambdaQueryWrapper.eq(EntryExitRecordEntity::getType, "inbound");
            List<EntryExitRecordEntity> entryExitRecordEntityList1 = entryExitRecordMapper.selectList(lambdaQueryWrapper);
            Map<String, List<EntryExitRecordEntity>> map = entryExitRecordEntityList1.stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getPartno));
            Iterator<Map.Entry<String, List<EntryExitRecordEntity>>> entries = map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, List<EntryExitRecordEntity>> entry = entries.next();
                Map<Object, List<EntryExitRecordEntity>> map0 = entry.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getWarehouseName())));
                Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries2 = map0.entrySet().iterator();
                while (entries2.hasNext()) {
                    Map.Entry<Object, List<EntryExitRecordEntity>> entry2 = entries2.next();
                    Map<Object, List<EntryExitRecordEntity>> map5 = entry2.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getOperator())));
                    Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries3 = map5.entrySet().iterator();
                    integers.add(map5.size());
                    if (personnelUsageAnalysisEntityList.size() < personnelUsageAnalysisReq.getPageSize()) {
                        while (entries3.hasNext()) {
                            Map.Entry<Object, List<EntryExitRecordEntity>> entry3 = entries3.next();

                            if (flag < endPage) {
                                flag++;
                            } else {
                                break;
                            }
                            if (flag > startPage) {
                                PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity = new PersonnelUsageAnalysisEntity();
                                personnelUsageAnalysisEntity.setLocation(entry2.getKey());
                                personnelUsageAnalysisEntity.setOperator(entry3.getKey());
                                personnelUsageAnalysisEntity.setType("刀具");
                                personnelUsageAnalysisEntity.setPartno(entry.getKey());
                                personnelUsageAnalysisEntity.setDate(entry3.getValue().get(0).getCreateTime());
                                LambdaQueryWrapper<ToolForkInfoEntity> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper3.eq(ToolForkInfoEntity::getPartno, entry.getKey());
                                lambdaQueryWrapper3.ne(ToolForkInfoEntity::getIdnr, "");
                                lambdaQueryWrapper3.ne(ToolForkInfoEntity::getType, "");
                                ToolForkInfoEntity toolPartEntities1 = toolForkInfoMapper.selectOne(lambdaQueryWrapper3);
//                        LambdaQueryWrapper<ToolPartEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
//                        lambdaQueryWrapper1.eq(ToolPartEntity::getPartno, entry.getKey());
//                        List<ToolPartEntity> toolPartEntityList = toolPartMapper.selectList(lambdaQueryWrapper1);
                                if (ObjectUtil.isNotEmpty(toolPartEntities1)) {
                                    personnelUsageAnalysisEntity.setIndr(toolPartEntities1.getIdnr());
                                    personnelUsageAnalysisEntity.setTypeName(typeItemMapper.getTypeNameByType(toolPartEntities1.getType()));

                                }

//                                Map<String, List<EntryExitRecordEntity>> map1 = entry3.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));

                                try {
                                    personnelUsageAnalysisEntity.setInputNum(entry3.getValue().stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));
                                } catch (Exception e) {
                                }
                                personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity);
                            }

                        }

                    }

                }
            }
        }
        if (StringUtils.isNotEmpty(personnelUsageAnalysisReq.getType()) && personnelUsageAnalysisReq.getType().equals("0")) {
            LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (personnelUsageAnalysisReq.getStartTime() != null && personnelUsageAnalysisReq.getEndTime() != null) {
                if (personnelUsageAnalysisReq.getStartTime().equals(personnelUsageAnalysisReq.getEndTime())) {
                    Date date = personnelUsageAnalysisReq.getEndTime();
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(date);
                    calendar.add(Calendar.DATE, 1);
                    date = calendar.getTime();
                    personnelUsageAnalysisReq.setEndTime(date);
                }
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, personnelUsageAnalysisReq.getStartTime(), personnelUsageAnalysisReq.getEndTime());
            } else {
                lambdaQueryWrapper.between(EntryExitRecordEntity::getCreateTime, LocalDate.now().minusMonths(1), LocalDate.now());
            }
            lambdaQueryWrapper.ne(EntryExitRecordEntity::getToolNo, "");
            lambdaQueryWrapper.eq(EntryExitRecordEntity::getType, "inbound");
            if (CollectionUtil.isNotEmpty(personnelUsageAnalysisReq.getPeopleList()))
                lambdaQueryWrapper.in(EntryExitRecordEntity::getOperator, personnelUsageAnalysisReq.getPeopleList());
            List<EntryExitRecordEntity> entryExitRecordEntityList1 = entryExitRecordMapper.selectList(lambdaQueryWrapper);
            Map<String, List<EntryExitRecordEntity>> map = entryExitRecordEntityList1.stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getToolNo));
            Iterator<Map.Entry<String, List<EntryExitRecordEntity>>> entries = map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, List<EntryExitRecordEntity>> entry = entries.next();
                Map<Object, List<EntryExitRecordEntity>> map0 = entry.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getWarehouseName())));
                Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries2 = map0.entrySet().iterator();
                while (entries2.hasNext()) {
                    Map.Entry<Object, List<EntryExitRecordEntity>> entry2 = entries2.next();
                    Map<Object, List<EntryExitRecordEntity>> map5 = entry2.getValue().stream().collect(Collectors.groupingBy(x -> Optional.ofNullable(x.getOperator())));
                    Iterator<Map.Entry<Object, List<EntryExitRecordEntity>>> entries3 = map5.entrySet().iterator();
                    integers.add(map5.size());
                    if (personnelUsageAnalysisEntityList.size() < personnelUsageAnalysisReq.getPageSize()) {
                        while (entries3.hasNext()) {
                            Map.Entry<Object, List<EntryExitRecordEntity>> entry3 = entries3.next();

                            if (flag < endPage) {
                                flag++;
                            } else {
                                break;
                            }
                            if (flag > startPage) {
                                PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity1 = new PersonnelUsageAnalysisEntity();
                                personnelUsageAnalysisEntity1.setDate(entry3.getValue().get(0).getCreateTime());
                                personnelUsageAnalysisEntity1.setLocation(entry2.getKey());
                                personnelUsageAnalysisEntity1.setOperator(entry3.getKey());
                                personnelUsageAnalysisEntity1.setType("总成");
                                personnelUsageAnalysisEntity1.setPartno(entry.getKey());

                                LambdaQueryWrapper<AssemblyToolInventoryEntity> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
                                lambdaQueryWrapper3.eq(AssemblyToolInventoryEntity::getToolNo, entry.getKey());
                                AssemblyToolInventoryEntity toolPartEntityList = assemblyToolInventoryMapper.selectOne(lambdaQueryWrapper3);
                                if (ObjectUtil.isNotEmpty(toolPartEntityList)) {
                                    personnelUsageAnalysisEntity1.setTypeName(toolPartEntityList.getToolType());
                                }

//                                Map<String, List<EntryExitRecordEntity>> map1 = entry3.getValue().stream().collect(Collectors.groupingBy(EntryExitRecordEntity::getType));

                                try {
                                    personnelUsageAnalysisEntity1.setInputNum(entry3.getValue().stream().collect(Collectors.summingInt(EntryExitRecordEntity::getNumber)));
                                } catch (Exception e) {
                                }
                                personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity1);
                            }

                        }

                    }

                }
            }
        }
        PageInfo<PersonnelUsageAnalysisEntity> result = new PageInfo<>(personnelUsageAnalysisEntityList);
        return new PageResult<>(result.getList(), (long) integers.stream().reduce(Integer::sum).orElse(0));
    }

    public Map<String, List<PersonnelUsageAnalysisEntity>> queryPersonnelToolUsageAnalysisChart(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {

//        LambdaQueryWrapper<EntryExitRecordEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (CollectionUtil.isNotEmpty(personnelUsageAnalysisReq.getPeopleList())) {
            queryWrapper.in(ICabinetRecordEntity::getOperator, personnelUsageAnalysisReq.getPeopleList());
        } else {
            List<String> list = new ArrayList<>();
            list.add("王双龙");
            list.add("李汝俊");
            queryWrapper.in(ICabinetRecordEntity::getOperator, list);
        }
        if (personnelUsageAnalysisReq.getStartTime() != null && personnelUsageAnalysisReq.getEndTime() != null) {
            if (personnelUsageAnalysisReq.getStartTime().equals(personnelUsageAnalysisReq.getEndTime())) {
                Date date = personnelUsageAnalysisReq.getEndTime();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(Calendar.DATE, 1);
                date = calendar.getTime();
                personnelUsageAnalysisReq.setEndTime(date);
            }
            queryWrapper.between(ICabinetRecordEntity::getTime, personnelUsageAnalysisReq.getStartTime(), personnelUsageAnalysisReq.getEndTime());
        } else {
            queryWrapper.between(ICabinetRecordEntity::getTime, LocalDate.now().minusDays(6), LocalDate.now());
        }
//        lambdaQueryWrapper.orderByDesc("create_time");
//        LocalDate date = LocalDate.now().minusDays(7);

        Map<String, List<PersonnelUsageAnalysisEntity>> listMap = new HashMap<>();
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
        entryExitRecordEntityList.forEach(entryExitRecordEntity -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            try {
                entryExitRecordEntity.setTime(sdf.parse(sdf.format(entryExitRecordEntity.getTime())));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        });
        Map<String, List<ICabinetRecordEntity>> map = entryExitRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getOperator));
        Iterator<Map.Entry<String, List<ICabinetRecordEntity>>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            List<Date> dates = new ArrayList();
            dates.add(personnelUsageAnalysisReq.getStartTime());
            Calendar calBegin = Calendar.getInstance();

            calBegin.setTime(personnelUsageAnalysisReq.getStartTime());
            while (personnelUsageAnalysisReq.getEndTime().after(calBegin.getTime())) {

                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                dates.add(calBegin.getTime());
            }
            Map.Entry<String, List<ICabinetRecordEntity>> entry = entries.next();

            Map<Date, List<ICabinetRecordEntity>> map0 = entry.getValue().stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getTime));
            Iterator<Map.Entry<Date, List<ICabinetRecordEntity>>> entries2 = map0.entrySet().iterator();
            List<PersonnelUsageAnalysisEntity> personnelUsageAnalysisEntityList = new ArrayList<>();
            while (entries2.hasNext()) {
                Map.Entry<Date, List<ICabinetRecordEntity>> entry1 = entries2.next();
                PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity = new PersonnelUsageAnalysisEntity();
                personnelUsageAnalysisEntity.setDate(entry1.getKey());
                dates.remove(entry1.getKey());
                Map<String, List<ICabinetRecordEntity>> map1 = entry1.getValue().stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getType));
                try {
                    personnelUsageAnalysisEntity.setInputNum(map1.get("加料单").stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c -> Integer.parseInt(c.getNumber())).sum());
                } catch (Exception e) {
                    personnelUsageAnalysisEntity.setInputNum(0);
                }
                try {
                    personnelUsageAnalysisEntity.setConsumeNum(map1.get("取料单").stream().filter(s -> StringUtils.isNotEmpty(s.getNumber())).mapToInt(c -> Integer.parseInt(c.getNumber())).sum());
                } catch (Exception e) {
                    personnelUsageAnalysisEntity.setConsumeNum(0);
                }
                personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity);
            }
            for (Date date : dates) {
                PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity1 = new PersonnelUsageAnalysisEntity();
                personnelUsageAnalysisEntity1.setDate(date);
                personnelUsageAnalysisEntity1.setInputNum(0);
                personnelUsageAnalysisEntity1.setConsumeNum(0);
                personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity1);
            }
            personnelUsageAnalysisEntityList = personnelUsageAnalysisEntityList.stream().sorted(Comparator.comparing(PersonnelUsageAnalysisEntity::getDate)).collect(Collectors.toList());
//            List<Student> studentsSortAsce = studentList.stream().sorted(Comparator.comparing(Student::getCreateTime)).collect(Collectors.toList());
            listMap.put(entry.getKey(), personnelUsageAnalysisEntityList);
            personnelUsageAnalysisReq.getPeopleList().remove(entry.getKey());
        }
        if (CollectionUtil.isNotEmpty(personnelUsageAnalysisReq.getPeopleList())) {
            personnelUsageAnalysisReq.getPeopleList().forEach(s -> {
                List<Date> dates = new ArrayList();
                dates.add(personnelUsageAnalysisReq.getStartTime());
                Calendar calBegin = Calendar.getInstance();

                calBegin.setTime(personnelUsageAnalysisReq.getStartTime());
                while (personnelUsageAnalysisReq.getEndTime().after(calBegin.getTime())) {

                    calBegin.add(Calendar.DAY_OF_MONTH, 1);
                    dates.add(calBegin.getTime());
                }
                List<PersonnelUsageAnalysisEntity> personnelUsageAnalysisEntityList = new ArrayList<>();
                for (Date date : dates) {
                    PersonnelUsageAnalysisEntity personnelUsageAnalysisEntity1 = new PersonnelUsageAnalysisEntity();
                    personnelUsageAnalysisEntity1.setDate(date);
                    personnelUsageAnalysisEntity1.setInputNum(0);
                    personnelUsageAnalysisEntity1.setConsumeNum(0);
                    personnelUsageAnalysisEntityList.add(personnelUsageAnalysisEntity1);
                }
                listMap.put(s, personnelUsageAnalysisEntityList);
            });
        }
//        while (!entries.hasNext()){
//
//        }
        return listMap;
    }

    public Object queryPersonnelToolUsageAnalysisChartReload(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) throws ParseException {
        Map<String, Object> retMap = new HashMap<>();
        //按照类型获取库存情况
        List<PersonalRecordDateVo> personalRecordDateList = new ArrayList<>();
        //获取日期范围
        List<String> dateList = CommonUtil.findEveryDay(personnelUsageAnalysisReq.getStartTime(), personnelUsageAnalysisReq.getEndTime());
        dateList.forEach(date -> {
            PersonalRecordDateVo personalRecordDateVo = new PersonalRecordDateVo();
            personalRecordDateVo.setDate(date);
            List<PersonalRecordVo> personalRecordList = new ArrayList<>();
            personnelUsageAnalysisReq.getPeopleList().forEach(operator -> {
                PersonalRecordVo personalRecordVo = new PersonalRecordVo();
                personalRecordVo.setOperator(operator);
                personalRecordVo.setNumber(icabinetRecordRepository.selectByOperatorAndType(operator, personnelUsageAnalysisReq.getType(), date));
                personalRecordList.add(personalRecordVo);
            });
            personalRecordDateVo.setList(personalRecordList);
            personalRecordDateList.add(personalRecordDateVo);
        });
        retMap.put("resultList", personalRecordDateList);
        retMap.put("operatorGroupNumber", icabinetRecordRepository.queryAllOperatorReload(personnelUsageAnalysisReq));
        return retMap;
    }


    public List<String> queryAllOperator() {
        LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
        queryWrapper.ne(ICabinetRecordEntity::getOperator, "");
        queryWrapper.groupBy(ICabinetRecordEntity::getOperator);
        List<ICabinetRecordEntity> entryExitRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
        List<String> strings = new ArrayList<>();
        entryExitRecordEntityList.forEach(iCabinetRecordEntity -> {
            strings.add(iCabinetRecordEntity.getOperator());
        });
        return strings;
    }

    public List<String> queryAllOperatorReload(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {
        List<PersonalRecordVo> personalRecordVos = icabinetRecordRepository.queryAllOperatorReload(personnelUsageAnalysisReq);
        if (CollectionUtil.isNotEmpty(personalRecordVos)) {
            return personalRecordVos.stream().map(PersonalRecordVo::getOperator).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }


    public Object queryPersonnelUsageConsumeIndex(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {
        List<RecordGroupOperatorIndexVo> groupIndexList = new ArrayList<>();
        List<RecordGroupOperatorIndexVo> pageList = new ArrayList<>();
        Map<String, Object> retMap = new HashMap<>();
        if (StringUtils.isNotBlank(personnelUsageAnalysisReq.getType()) && Objects.equals(personnelUsageAnalysisReq.getType(), "1")) {
            groupIndexList = entryExitRecordRepository.selectIndex(personnelUsageAnalysisReq);
            if (CollectionUtil.isNotEmpty(groupIndexList)) {
                pageList = CommonUtil.pageList(groupIndexList, personnelUsageAnalysisReq.getPageNo(), personnelUsageAnalysisReq.getPageSize());
                //设置多余信息
                pageList.forEach(recordGroupOperatorIndexVo -> {
                    recordGroupOperatorIndexVo.setTypeName(toolForkInfoRespository.selectTypeNameByPartno(recordGroupOperatorIndexVo.getPartno()));
                    recordGroupOperatorIndexVo.setIdnr(toolForkInfoRespository.selectPartTypeByPartno(recordGroupOperatorIndexVo.getPartno()));
                });
            }
        } else if (StringUtils.isNotBlank(personnelUsageAnalysisReq.getType()) && Objects.equals(personnelUsageAnalysisReq.getType(), "0")) {
            groupIndexList = entryExitRecordRepository.selectIndexReload(personnelUsageAnalysisReq);
            if (CollectionUtil.isNotEmpty(groupIndexList)) {
                pageList = CommonUtil.pageList(groupIndexList, personnelUsageAnalysisReq.getPageNo(), personnelUsageAnalysisReq.getPageSize());
                //设置多余信息
                pageList.forEach(recordGroupOperatorIndexVo -> {
                    recordGroupOperatorIndexVo.setTypeName(assemblyToolInventoryRepository.selectTypeByToolNo(recordGroupOperatorIndexVo.getToolNo()));
                });
            }
        }
        retMap.put("list", pageList);
        retMap.put("total", groupIndexList.size());
        if (CollectionUtil.isNotEmpty(groupIndexList)) {
            retMap.put("peopleList", groupIndexList.stream().map(RecordGroupOperatorIndexVo::getOperator).distinct().collect(Collectors.toList()));
        } else {
            retMap.put("peopleList", new ArrayList<>());
        }
        return retMap;
    }

    public Object queryPersonnelUsageConsumeDetail(PersonnelUsageAnalysisReq personnelUsageAnalysisReq) {
        Map<String, Object> retMap = new HashMap<>();
        PageHelper.startPage(personnelUsageAnalysisReq.getPageNo() , personnelUsageAnalysisReq.getPageSize());
        PageInfo<EntryExitRecordEntity> pageInfo = new PageInfo<>(entryExitRecordRepository.selectListByCondition(personnelUsageAnalysisReq));
        if (StringUtils.isNotBlank(personnelUsageAnalysisReq.getType()) && Objects.equals(personnelUsageAnalysisReq.getType(), "1")) {
            if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
                //设置多余信息
                pageInfo.getList().forEach(entryExitRecord -> {
                    entryExitRecord.setTypeName(toolForkInfoRespository.selectTypeNameByPartno(entryExitRecord.getPartno()));
                    entryExitRecord.setIdnr(toolForkInfoRespository.selectPartTypeByPartno(entryExitRecord.getPartno()));
                });
            }
        } else if (StringUtils.isNotBlank(personnelUsageAnalysisReq.getType()) && Objects.equals(personnelUsageAnalysisReq.getType(), "0")) {
            if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
                //设置多余信息
                pageInfo.getList().forEach(recordGroupOperatorIndexVo -> {
                    recordGroupOperatorIndexVo.setTypeName(assemblyToolInventoryRepository.selectTypeByToolNo(recordGroupOperatorIndexVo.getToolNo()));
                });
            }
        }
        retMap.put("list", pageInfo.getList());
        retMap.put("total", pageInfo.getTotal());
        if (CollectionUtil.isNotEmpty(pageInfo.getList())) {
            retMap.put("peopleList", pageInfo.getList().stream().map(EntryExitRecordEntity::getOperator).distinct().collect(Collectors.toList()));
        } else {
            retMap.put("peopleList", new ArrayList<>());
        }
        return retMap;
    }
}
